//
//  MYMySetVC.m
//  MuYing
//
//  Created by tbb on 2019/8/7.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYMySetVC.h"
#import "FileTool.h"
#import "YQMYBaseNavigationController.h"
#import "MYLoginVC.h"
#import "MYMySetMsgWarnVC.h"
#define CachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
@interface MYMySetVC ()
@property (nonatomic, assign) NSInteger memoryTotalSize;

@end

@implementation MYMySetVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isNavShadow = YES;
    self.navigationItem.title = kYQLS(@"设置");
    // 获取文件夹尺寸
    // 文件夹非常小,如果我的文件非常大
    [FileTool getFileSize:CachePath completion:^(NSInteger totalSize) {
        self.memoryTotalSize = totalSize;
        [self.listTableView reloadData];
    }];
}

-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    kYQREGISTERTABLEVIEWNIBCELL(@"MYMySetLoginOutCell");
    kYQREGISTERTABLEVIEWNIBCELL(@"MYMySetTextCell");
    kYQREGISTERTABLEVIEWCELL([YQAddressAddSwitchCell class])
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;//4
    }
    return 1;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0 || indexPath.row == 1) {
            MYMySetTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"MYMySetTextCell"];
            textCell.subLb.text = @"";
            if (indexPath.row == 0) {
                textCell.titleLb.text = kYQLS(@"账号安全");
            }else{
                textCell.titleLb.text = kYQLS(@"消息提醒");
            }
            return textCell;
        }else{
            YQAddressAddSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQAddressAddSwitchCell"];
            cell.textField.textInsets = UIEdgeInsetsMake(0, 0, 0, 0);
            cell.isTextFieldEdit = false;
            //cell.textField.placeholder = self.leftTextArr.lastObject;
            if (indexPath.row == 2) {
                cell.textField.text = kYQLS(@"开启定位服务");
            }else{
                cell.textField.text = kYQLS(@"Wi-Fi下自动播放视频");
            }
            return cell;
        }
    }else if (indexPath.section == 1) {
        MYMySetTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"MYMySetTextCell"];
        textCell.subLb.text = [self sizeStr];
        textCell.titleLb.text = kYQLS(@"清除缓存");
        return textCell;
    }else if (indexPath.section == 2) {
        MYMySetTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"MYMySetTextCell"];
        textCell.subLb.text = @"";
        textCell.titleLb.text = kYQLS(@"给个好评");
        return textCell;
    }else{
        MYMySetLoginOutCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYMySetLoginOutCell"];
        return cell;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 58;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            MYMyAccountsSafetyVC *safetyVC = [[MYMyAccountsSafetyVC alloc] init];
            [safetyVC setModel:self.model];
            kYQPUSH(safetyVC);
        }else if(indexPath.row == 1) {
            MYMySetMsgWarnVC *warnVC = [[MYMySetMsgWarnVC alloc] init];
            kYQPUSH(warnVC);
        }

    }else if(indexPath.section == 1) {
        
        if (self.memoryTotalSize > 0) {
            
            QMUIAlertController *alert = [[QMUIAlertController alloc] initWithTitle:kYQLS(@"确定清除缓存吗?") message:@"" preferredStyle:(QMUIAlertControllerStyleAlert)];
            [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"取消") style:(QMUIAlertActionStyleCancel) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
                
            }]];
            alert.alertCancelButtonAttributes = @{NSForegroundColorAttributeName:kAPPMainTitleColor,NSFontAttributeName:UIFontBoldMake(14),NSKernAttributeName:@(0)};
            [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"确定") style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
                [self escCoachClick];
            }]];
            alert.alertButtonAttributes = @{NSForegroundColorAttributeName:kAPPThemeColor,NSFontAttributeName:UIFontBoldMake(14),NSKernAttributeName:@(0)};
            [alert showWithAnimated:YES];
            
        }
    }else if(indexPath.section == 3) {
        QMUIAlertController *alert = [[QMUIAlertController alloc] initWithTitle:kYQLS(@"确定退出登录吗?") message:@"" preferredStyle:(QMUIAlertControllerStyleAlert)];
        [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"取消") style:(QMUIAlertActionStyleCancel) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
            
        }]];
        alert.alertCancelButtonAttributes = @{NSForegroundColorAttributeName:kAPPMainTitleColor,NSFontAttributeName:UIFontBoldMake(14),NSKernAttributeName:@(0)};
        [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"确定") style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
            
        
            [YQPNetWorkingManager sendPostRequest:kPOST_LOGOUT_URL parameters:nil success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
                [MBProgressHUD showSuccess:kYQLS(@"成功")];
                [YQPUserTool deleteAccount];
                MYLoginVC *loginVc = [[MYLoginVC alloc] init];
                YQMYBaseNavigationController *navVC = [[YQMYBaseNavigationController alloc] initWithRootViewController:loginVc];
                [UIApplication sharedApplication].keyWindow.rootViewController = navVC;
            } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
                [YQPUserTool deleteAccount];
                [MBProgressHUD showSuccess:kYQLS(@"成功")];
                MYLoginVC *loginVc = [[MYLoginVC alloc] init];
                YQMYBaseNavigationController *navVC = [[YQMYBaseNavigationController alloc] initWithRootViewController:loginVc];
                [UIApplication sharedApplication].keyWindow.rootViewController = navVC;
            }];
            
        }]];
        alert.alertButtonAttributes = @{NSForegroundColorAttributeName:kAPPThemeColor,NSFontAttributeName:UIFontBoldMake(14),NSKernAttributeName:@(0)};
        [alert showWithAnimated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

// 获取缓存尺寸字符串
- (NSString *)sizeStr
{
    NSInteger totalSize = _memoryTotalSize;
    NSString *sizeStr = @"";
    // MB KB B
    if (totalSize > 1000 * 1000) {
        // MB
        CGFloat sizeF = totalSize / 1000.0 / 1000.0;
        sizeStr = [NSString stringWithFormat:@"%.1fMB",sizeF];
    } else if (totalSize > 1000) {
        // KB
        CGFloat sizeF = totalSize / 1000.0;
        sizeStr = [NSString stringWithFormat:@"%.1fKB",sizeF];
    } else if (totalSize > 0) {
        // B
        sizeStr = [NSString stringWithFormat:@"%.ldB",totalSize];
    }else{
        sizeStr = @"0B";
    }
    
    return sizeStr;
}
- (void)escCoachClick {
    [UIView animateWithDuration:1.5 animations:^{
        [QMUITips showLoadingInView:self.view];
    } completion:^(BOOL finished) {
        [QMUITips hideAllTipsInView:self.view];
        [QMUITips showSucceed:kYQLS(@"清除成功") inView:self.view hideAfterDelay:1];
        [self clearCacheClick];
    }];
}

- (void)clearCacheClick
{
    // 清空缓存
    // 删除文件夹里面所有文件
    [FileTool removeDirectoryPath:CachePath];
    // 删除缓存之后,文件大小显示为0
    self.memoryTotalSize = 0;
    [self.listTableView reloadData];
}
@end
